/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gui;

import app.gui.scene.SceneHolder;
import app.gui.scene.SceneObserver;
import com.app.ThreadManager;
import com.app.client.BroadcastListener;
import com.app.observers.HostListSubject;
import com.app.server.Broadcast;
import com.app.task.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

/**
 *
 * @author sammael
 */
public class MultiPlayerMenu extends SceneHolder {
    
    private SceneHolder hostHolder,joinHolder;
    private Button joinButton,hostButton,backButton;
    //private Task broadcast = new Broadcast(9090);
    private Task broadListener = new BroadcastListener(9090) ;
    
    
    public MultiPlayerMenu(SceneHolder prevHolder){
        this.previousHolder = prevHolder;
        VBox vb = new VBox(spacing);
        vb.setAlignment(Pos.CENTER);
        joinButton = new Button("Dołącz");
        joinButton.setMaxWidth(buttonWidth);
        joinButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                if(joinHolder == null){
                    HostList hl = new HostList(MultiPlayerMenu.this);
                    ((HostListSubject)broadListener).registerObserver(hl); 
                    joinHolder = hl;
                    for(SceneObserver ob : observers){
                        joinHolder.registerObserver(ob);
                    }
                }
                //broadcast.stop();
                nextScene = joinHolder.getScene();
                
                ThreadManager.getInstance().execute(broadListener);
                notifyObservers();
            }
        });
        hostButton = new Button("Stwórz");
        hostButton.setMaxWidth(buttonWidth);
        hostButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                if(hostHolder==null){
                    hostHolder = new PlayerDialog(MultiPlayerMenu.this, false);
                    for(SceneObserver ob: observers){
                        hostHolder.registerObserver(ob);
                    }
                   
                }
                 //broadListener.stop();
                 //ThreadManager.getInstance().execute(broadcast);
                 nextScene = hostHolder.getScene();
                 notifyObservers();
            }
        });
        
        backButton = new Button("Powrót");
        backButton.setMaxWidth(buttonWidth);
        backButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                nextScene = MultiPlayerMenu.this.previousHolder.getScene();
                notifyObservers();
            }
        });
        HelpButton helpButton = new HelpButton();
    	AnchorPane anchor = new AnchorPane();
    	AnchorPane.setBottomAnchor(helpButton, 10.0);
    	AnchorPane.setRightAnchor(helpButton, 10.0);
    	anchor.getChildren().setAll(helpButton);
    	 vb.getChildren().setAll(hostButton,joinButton,backButton);
        BorderPane border = new BorderPane();
        border.setCenter(vb);
        border.setBottom(anchor);
        
        scene = new Scene(border, rootWidth, rootHeigh);
    }

}
